package com.wang.interview.designmode.proxy.JDK;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author: wang
 * @date: 2021/8/1 16:22
 * @description:
 * 注意：
 * 1、T t = clazz.newInstance();
 * 2、method.invoke(t, args);
 */
public class DynamicProxy {

    public <T> T getProxy(Class<T> clazz) throws IllegalAccessException, InstantiationException {
        T t = clazz.newInstance();
        return (T)Proxy.newProxyInstance(t.getClass().getClassLoader(), t.getClass().getInterfaces(), new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                System.out.println("代理开始");
                Object invoke = method.invoke(t, args);
                System.out.println("代理结束");
                return invoke;
            }
        });
    }
}
